package com.jiujichaoshi.oauth.server.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * Description: 查询用户，<code>username</code>是唯一性依据，可以是账号，手机号，邮箱，openId
 *
 * @author YangLong [410357434@163.com]
 * @version V1.0
 * @date 2021/2/5
 */
@Primary
@Service
public class ExtendUserDetailsServiceImpl implements UserDetailsService {
    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //从数据库查询
        List<GrantedAuthority> authorities = Stream.of("ADMIN","USER").map(SimpleGrantedAuthority::new).collect(Collectors.toList());
        User user=new User("zhangsan",passwordEncoder.encode("123456"),true,true,true,true,authorities);
        return user;
    }
}
